package com.hzcx.app.simplechat.ui.publicui.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

//import com.facebook.drawee.view.SimpleDraweeView;
import com.hzcx.app.simplechat.ui.group.bean.MembersBean;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.util.glide.GlideUtils;

import java.util.List;

import okhttp3.internal.Util;

public class GroupSettingGridAdapter extends BaseAdapter {

    private Context mContext;
    private List<MembersBean> userList;

    public boolean showDele = false;
    public boolean showAdd = true;

    public GroupSettingGridAdapter(Context context, List<MembersBean> userList) {
        this.mContext = context;
        this.userList = userList;
    }

    @Override
    public int getCount() {
        int count = 0;
        if (showAdd) {
            count++;
        }
        if (showDele) {
            count++;
        }
        return userList.size() + count;
    }


    @Override
    public MembersBean getItem(int position) {
        if (userList.size() > position) {
            return userList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_chat_setting_gridview, null);
            viewHolder  = new ViewHolder();
            viewHolder.mAvatarSdv = convertView.findViewById(R.id.sdv_avatar);
            viewHolder.mNickNameTv = convertView.findViewById(R.id.tv_nick_name);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        MembersBean user = getItem(position);
        if (user != null && user.getMember() != null) {
            GlideUtils.loadImg(mContext, user.getMember().getAvatar(), viewHolder.mAvatarSdv);
            viewHolder.mNickNameTv.setVisibility(View.VISIBLE);
            viewHolder.mNickNameTv.setText(user.getMember().getNickname());
        }
        if (showAdd && showDele) {
            if (position == getCount() - 2) {
                // 加人
                viewHolder.mAvatarSdv.setImageResource(R.mipmap.ic_chat_group_member_add);
                viewHolder.mNickNameTv.setVisibility(View.INVISIBLE);
                return convertView;
            } else if (position == getCount() - 1) {
                // 减人
                viewHolder.mAvatarSdv.setImageResource(R.mipmap.ic_chat_group_member_del);
                viewHolder.mNickNameTv.setVisibility(View.INVISIBLE);
                return convertView;
            }
        } else if (showAdd) {
            if (position == getCount() - 1) {
                // 加人
                viewHolder.mAvatarSdv.setImageResource(R.mipmap.ic_chat_group_member_add);
                viewHolder.mNickNameTv.setVisibility(View.INVISIBLE);
                return convertView;
            }
        } else {
            if (position == getCount() - 1) {
                // 减人
                viewHolder.mAvatarSdv.setImageResource(R.mipmap.ic_chat_group_member_del);
                viewHolder.mNickNameTv.setVisibility(View.INVISIBLE);
                return convertView;
            }
        }



        return convertView;
    }

    private static class ViewHolder {
        public ImageView mAvatarSdv;
        public TextView mNickNameTv;
    }
}
